package com.joysuch.wwyt.bp.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.gson.JsonObject;
import com.joysuch.wwyt.bp.bean.BpPermitsBean;
import com.joysuch.wwyt.bp.entity.BpPermits;
import com.joysuch.wwyt.bp.repository.BpPermitsDao;
import com.joysuch.wwyt.bp.service.BpPermitsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.expression.spel.ast.OpNE;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BpPermitsServiceImpl implements BpPermitsService {

    @Autowired
    private BpPermitsDao dao;


    @Override
    public Page<BpPermits> page(Pageable page) {
        Page<BpPermits> all = dao.findAll(page);
        return all;
    }

    @Override
    public void delete(Long id) {
        BpPermits one = dao.getOne(id);
        one.setIsThere("N");
        one.setInitiatingDate(null);
        one.setTerminatesDate(null);
        one.setPermits(null);
        dao.save(one);
    }

    @Override
    public void add(BpPermitsBean bpPermits) throws JsonProcessingException{

        BpPermits one = dao.getOne(bpPermits.getId());
       ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(bpPermits);
       System.out.println(s);
       one.setPermits(s);
        one.setIsThere("Y");
        one.setTerminatesDate(bpPermits.getValidityEnd());
        one.setInitiatingDate(bpPermits.getValidityBegin());
        dao.save(one);
    }


}
